# -*- coding: utf8 -*-


import time
import json
import pymongo

from scrapy import Spider
from scrapy.http import Request


class Exchange(Spider):
    name = 'info'

    def __init__(self, name = None, **kwargs):
        super(Exchange, self).__init__(name, **kwargs)

        client = pymongo.MongoClient('47.254.26.182', 27017)
        self.db = client.bt_news
        # 获取集合名

    def start_requests(self):
        url = 'https://block.cc/api/v1/exchange/list?size=300'

        yield Request(
            url = url,
            dont_filter=True,
            headers = {
                'Host': 'block.cc',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0',
            },
            meta = {

            },
            callback = self.exchange_info
        )

    def exchange_info(self, response):
        # 获取json中的数据，对应交易所信息
        body = json.loads(response.body)
        # self.log('body: %s' % body)

        data = body.get('data')
        list = data.get('list')

        for exchange in list:
            # 交易所名称
            name = exchange.get('name')
            if name == 'bittrex':

                # 交易所别名
                display_name = exchange.get('display_name')

                # 交易所logo
                logo = 'https://blockchains.oss-cn-shanghai.aliyuncs.com/static/Exchange/%s.png' % name

                # 交易所地址
                home_url = 'https://%s.com/' % name

                # 交易量
                volume = exchange.get('volume_BTC')

                # 所属国家中文
                country = exchange.get('country')
                belong_cn = country.get('name') if country != None else None

                # 所属国家英文
                belong_en = country.get('en_name') if country != None else None

                # 交易所简介
                text = exchange.get('text')
                comments = text.get('zh') if text != None else None

                # 1现货，2期货，3法币
                if exchange.get('exchangeType'):
                    sup = exchange.get('exchangeType')
                    support = ",".join(sup)
                else:
                    support = 0

                self.log('support: %s' % support)

                # 星级
                star = exchange.get('star')

                # 0正常，1删除
                is_del = 0

                # 入库
                data = {
                    'name': name,
                    'display_name': display_name,
                    'logo': logo,
                    'home_url': home_url,
                    'volume': volume,
                    'belong_cn': belong_cn,
                    'belong_en': belong_en,
                    'comments': comments,
                    'support': support,
                    'star': star,
                    'is_del': is_del,
                    'addtime': time.localtime()
                }
                self.db.bt_exchange.update_one({'name':name},{'$set':data}, upsert=True)

    # # close
    # def close(spider, reason):
    #     spider.sql.commit()

# 数据库结构
'''
CREATE TABLE `bt_exchange` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL COMMENT '交易所名称',
  `display_name` varchar(50) DEFAULT NULL COMMENT '别名',
  `logo` varchar(256) DEFAULT NULL COMMENT '交易所logo',
  `home_url` varchar(256) DEFAULT NULL COMMENT '交易所地址',
  `volume` varchar(50) DEFAULT NULL COMMENT '交易量',
  `belong_cn` varchar(50) DEFAULT NULL COMMENT '所属国家中文',
  `belong_en` varchar(50) DEFAULT NULL COMMENT '所属国家英文',
  `comments` text COMMENT '交易所简介',
  `support` varchar(20) DEFAULT NULL COMMENT '1现货，2期货，3法币',
  `star` int(1) DEFAULT NULL COMMENT '星级',
  `is_del` tinyint(1) DEFAULT '0' COMMENT '0正常，1删除',
  `addtime` timestamp NULL DEFAULT NULL COMMENT '添加时间',
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='交易所';
'''